// 找到所有红色表格中的td
var alltds = document.querySelectorAll("#board2 td");
// 当前回合该哪方下
var turn = "black";
var round = document.getElementById("round");
var his = [];

// td的点击事件
function tdClick(e){
    if(finish){
        return;
    }
    var td = e.target;
    var side = td.getAttribute("chess");
    if(side){
        return;
    }

    td.setAttribute("chess",turn);
    winCheck(e.target.index);




    // e.target.style.backfroundColor = "red";
    if(turn=="black"){
        turn = "white";
        round.textContent = "白方回合";
        

    }else{
        turn = "black";
        round.textContent = "黑方回合";
    }
   his.push(e.target);


}

for(var i = 0; i < alltds.length;i++){
    alltds[i].onclick = tdClick;
    alltds[i].index = i;

}
function back(){
    finish = false;
    if(his.length>0){
        var td = his.pop();
        td.removeAttribute("chess");
        turn = turn=="black"?"white":"black";
        round.textContent = turn == "black"? "黑方":"白方";
    }
}
function restart(){
   window.location.reload(); 
}
// 判断是否胜利
function winCheck(index){
    if(horizontalCheck(index)||verticalCheck(index)||sssCheck(index)||tlbrCheck(index)){
        finish = true;
        alert((turn=="black"?"黑":"白")+"方胜利");
    }

    }

// 横向检查函数
function horizontalCheck(index){
    var num = 1 ;

    var currentIndex = index;
    var hasNext = currentIndex%19!=0&&alltds[currentIndex-1].getAttribute("chess")==turn;
    while(hasNext){
        num++;
        currentIndex--;
    var hasNext = currentIndex%19!=0&&alltds[currentIndex-1].getAttribute("chess")==turn;
    
} 
 var currentIndex = index;
    var hasNext = currentIndex%19!=18&&alltds[currentIndex+1].getAttribute("chess")==turn;
    while(hasNext){
        num++;
        currentIndex++;
    var hasNext = currentIndex%19!=18&&alltds[currentIndex+1].getAttribute("chess")==turn;
    
} 
return num>=5;

}
// 纵向检查函数
function verticalCheck(index){
    var num = 1 ;

    var currentIndex = index;
    var hasNext = currentIndex>=19&&alltds[currentIndex-19].getAttribute("chess")==turn;
    while(hasNext){
        num++;
        currentIndex-=19;
    var hasNext = currentIndex>=19&&alltds[currentIndex-19].getAttribute("chess")==turn;
    
} 
 var currentIndex = index;
    var hasNext = currentIndex<342&&alltds[currentIndex+19].getAttribute("chess")==turn;
    while(hasNext){
        num++;
        currentIndex+=19;
    var hasNext = currentIndex<342&&alltds[currentIndex+19].getAttribute("chess")==turn;
    
} 
return num>=5;

}
// 左上到右下斜线检查
function sssCheck(index){
    var num = 1 ;

    var currentIndex = index;
    var hasNext = currentIndex%19!=0&&currentIndex>=19&&alltds[currentIndex-20].getAttribute("chess")==turn;
    while(hasNext){
        num++;
        currentIndex-=20;
    var hasNext = currentIndex%19!=0&&currentIndex>=19&&alltds[currentIndex-20].getAttribute("chess")==turn;
    
} 
 var currentIndex = index;
    var hasNext = currentIndex<342&&currentIndex%19!=18&&alltds[currentIndex+20].getAttribute("chess")==turn;
    while(hasNext){
        num++;
        currentIndex+=20;
    var hasNext = currentIndex<342&&currentIndex%19!=18&&alltds[currentIndex+20].getAttribute("chess")==turn;
    
} 
return num>=5;

}
// 右上到左下的检查函数
function tlbrCheck(index){
    var num = 1 ;

    var currentIndex = index;
    var hasNext = currentIndex%19!=18&&currentIndex>=18&&alltds[currentIndex-18].getAttribute("chess")==turn;
    while(hasNext){
        num++;
        currentIndex-=18;
    var  hasNext = currentIndex%19!=18&&currentIndex>=18&&alltds[currentIndex-18].getAttribute("chess")==turn;
    
} 
 var currentIndex = index;
    var hasNext = currentIndex<342&&currentIndex%19!=0&&alltds[currentIndex+18].getAttribute("chess")==turn;
    while(hasNext){
        num++;
        currentIndex+=18;
    var hasNext = currentIndex<342&&currentIndex%19!=0&&alltds[currentIndex+18].getAttribute("chess")==turn;
    
} 
return num>=5;

}
var finish = false;
